package black.remote.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import black.market.main.BlackPublic;
import black.market.main.BlackPublic.Action;

public class RemoteService extends Service {
	public static final String TAG = "RemoteService";
	private Context mContext = null;
	private AlarmManager mAlarmManager = null;
	private boolean isReadLocationAlarm = false;
	private boolean isKmaAlarm = false;
	BlackAIDL.Stub mStub = new BlackAIDL.Stub() {

		@Override
		public boolean isBinderAlive() {
			// TODO Auto-generated method stub
			return super.isBinderAlive();
		}

		public void readLocationAlarm1Hour_Repeating() {
			isReadLocationAlarm = true;
			BlackPublic.identifiesLog(TAG, "readLocationAlarm1Hour_Repeating -> isReadLocationAlarm : " + isReadLocationAlarm);
			Intent intent = new Intent();
			intent.setAction(Action.REMOTE_READLOCATION_1H.toString());
			PendingIntent mPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			BlackPublic.debugLog(TAG, "mPendingIntent:" + mPendingIntent.getTargetPackage());
			mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, mPendingIntent);
		}

		public void kmaAlarm3Hour_InexactRepeating() {
			isKmaAlarm = true;
			BlackPublic.identifiesLog(TAG, "kmaAlarm3Hour_InexactRepeating -> isKmaAlarm : " + isKmaAlarm);
			Intent intent = new Intent();
			intent.setAction(Action.REMOTE_KMA_DATA_INTERVAL3H.toString());
			PendingIntent mPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_HOUR * 3, mPendingIntent);
		}
	};

	public boolean isReadLocationAlarm() {
		return isReadLocationAlarm;
	}

	public boolean isKmaAlarm() {
		return isKmaAlarm;
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		BlackPublic.debugLog(TAG, "onBind");
		return mStub;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		BlackPublic.debugLog(TAG, "onCreate");
		mContext = getApplicationContext();
		mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		isReadLocationAlarm = false;
		isKmaAlarm = false;
		BlackPublic.identifiesLog(TAG, "Remote Service Destroy -> isReadLocationAlarm : " + isReadLocationAlarm + " / isKmaAlarm : " + isKmaAlarm);
		super.onDestroy();
	}

	public void readLocationAlarm1Hour_Repeating() {
		isReadLocationAlarm = true;
		BlackPublic.identifiesLog(TAG, "readLocationAlarm1Hour_Repeating -> isReadLocationAlarm : " + isReadLocationAlarm);
		Intent intent = new Intent();
		intent.setAction(Action.REMOTE_READLOCATION_1H.toString());
		PendingIntent mPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		BlackPublic.debugLog(TAG, "mPendingIntent:" + mPendingIntent.getTargetPackage());
		AlarmManager mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, mPendingIntent);
	}

	public void kmaAlarm3Hour_InexactRepeating() {
		isKmaAlarm = true;
		BlackPublic.identifiesLog(TAG, "kmaAlarm3Hour_InexactRepeating -> isKmaAlarm : " + isKmaAlarm);
		Intent intent = new Intent();
		intent.setAction(Action.REMOTE_KMA_DATA_INTERVAL3H.toString());
		PendingIntent mPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		mAlarmManager.setRepeating(AlarmManager.RTC, AlarmManager.INTERVAL_HOUR * 2, 30000, mPendingIntent);
	}

	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		super.onStartCommand(intent, flags, startId);
		kmaAlarm3Hour_InexactRepeating();
		readLocationAlarm1Hour_Repeating();
		return START_NOT_STICKY;
	}
}
